//
//  ContractStoreViewController.m
//  Beauty
//
//  Created by Snow on 16/9/20.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ContractStoreViewController.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "ModelOfStore.h"
#import "StoreTableViewCell.h"
#import "DesignOfStoreViewController.h"
@interface ContractStoreViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrayOfStore;
@end

@implementation ContractStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createView];
    }
- (void)handleData
{
    _arrayOfStore = [NSMutableArray array];
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSString *password = [userdefault objectForKey:@"password"];
    NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile/index.php?act=member_favorites&op=store_list&curpage=1&key=%@&page=10", password];
    [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
        NSArray *array = responseObject[@"datas"][@"favorites_list"];
        for (NSDictionary *dic in array) {
            ModelOfStore *model = [[ModelOfStore alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_arrayOfStore addObject:model];
        }
        [_tableView reloadData];
    } filed:^(NSError *error) {
        
    }];
}
- (void)createView
{
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[StoreTableViewCell class] forCellReuseIdentifier:@"StoreTableViewCell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrayOfStore.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoreTableViewCell"];
    ModelOfStore *model = [_arrayOfStore objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.store_avatar]];
    cell.labelOfName.text = model.store_name;
    cell.labelOfContent.text = model.member_name;
    cell.labelOfContent.font = [UIFont systemFontOfSize:13];
    cell.labelOfContent.textColor = [UIColor lightGrayColor];
    cell.button.tintColor = [UIColor grayColor];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfStore *model = [_arrayOfStore objectAtIndex:indexPath.row];
    DesignOfStoreViewController *store = [[DesignOfStoreViewController alloc]init];
    store.store_id = model.store_id;
    [self.navigationController pushViewController:store animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
